﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Td.Additional.ComponentModel
{
    /// <summary>
    /// Predefined list attribute class.
    /// See also http://www.mycsharp.de/wbb2/thread.php?postid=301367#post301367.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
    public sealed class PredefinedListAttribute : Attribute
    {
        #region Properties

        /// <summary>
        /// Gets or sets the elements.
        /// </summary>
        /// <value>The elements.</value>
        public List<Object> Elements { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PredefinedListAttribute"/> class.
        /// </summary>
        private PredefinedListAttribute()
            : base()
        {
            Elements = new List<object>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PredefinedListAttribute"/> class.
        /// </summary>
        /// <param name="elements">The elements.</param>
        public PredefinedListAttribute(params Object[] elements)
            : this()
        {
            Elements.AddRange(elements);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PredefinedListAttribute"/> class.
        /// </summary>
        /// <param name="enumeration">The enumeration.</param>
        public PredefinedListAttribute(Type enumeration)
            : this()
        {
            if (!enumeration.IsEnum)
                throw new ArgumentOutOfRangeException("enumeration", "Type must be an enumeration");
            foreach (var item in Enum.GetValues(enumeration))
                Elements.Add(item);
        }

        #endregion
    }
}